package com.heima.es.utils;

import com.alibaba.fastjson.JSON;
import com.heima.es.pojo.GoodInfo;
import com.heima.es.pojo.Result;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;

import java.io.IOException;
import java.lang.annotation.*;
import java.util.ArrayList;
import java.util.List;

public class ElasticUtils<T> {

    private RestHighLevelClient client;
    private String indexName;
    private Class<T> clazz;

    public ElasticUtils(RestHighLevelClient client, String indexName, Class<T> clazz) {
        this.client = client;
        this.indexName = indexName;
        this.clazz = clazz;
    }

    //createIndex(String source)
    //deleteIndex()
    //save(T t)
    //save(List<T> list)
    //T getById(String id)
    //PageResult<T> search(SearchSourceBuilder sourceBuilder)
    public Result searchPageInfo(Integer page, Integer size, String keyword) throws IOException {
        Result result=new Result();

        //构建高亮三要素
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("name").preTags("<em>").postTags("</em>");

        //构建查询条件SearchSourceBuilder
        SearchSourceBuilder builder=new SearchSourceBuilder();
        builder.from((page-1)*size)
                .size(size)
                .highlighter(highlightBuilder)
                .query(
                QueryBuilders.boolQuery()
                .must(QueryBuilders.matchQuery("name",keyword))
        );

        //构建查询请求SearchRequest
        SearchRequest request=new SearchRequest(indexName);
        request.source(builder);

        //发送请求并拿到响应结果
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchHits searchHits = response.getHits();

        //封装结果
        Long total=searchHits.getTotalHits().value;
        result.setTotal(total);
        result.setPages(total%size==0?total/size:(total/size+1));
        List<GoodInfo> goods = new ArrayList<>();
        for (SearchHit searchHit : searchHits) {
            GoodInfo goodInfo= JSON.parseObject(searchHit.getSourceAsString(),GoodInfo.class);
            HighlightField field = searchHit.getHighlightFields().get("name");
            Text[] fragments = field.getFragments();
            for (Text fragment : fragments) {
                String name=fragment.string();
                if (name!=null&&name.length()>0){
                    goodInfo.setName(name);
                    goods.add(goodInfo);
                }
            }
        }
        result.setData(goods);
        return result;
    }
    //private String getId(Object t)

    @Target({ElementType.FIELD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface EsId{

    }
}
